package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.dto.RelationAttrInfoDTO;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductAttribute;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductCategory;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品属性参数表 前端控制器
 * </p>
 *
 * @author Miao
 * @since 2022-05-19
 */
@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {

    @Autowired
    PmsProductAttributeService productAttributeService;

    /**
     * 商品分类—商品属性数据列表
     * url:'/productAttribute/list/'+cid,
     * method:'get',
     * params:params
     */
    @RequestMapping(value = "/list/{cid}",method = RequestMethod.GET)
    public CommonResult<CommonPage> getList(@PathVariable Long cid,
                                            @RequestParam(value = "type") Integer type,
                                            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        Page page=productAttributeService.list(cid,type,pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(page));
    }

    /**
     * 根据商品分类id获取关联的筛选属性
     * Request URL: http://localhost:8099/productAttribute/attrInfo/1
     * Request Method: GET
     *
     */
    @RequestMapping(value = "/attrInfo/{cId}",method = RequestMethod.GET)
    public CommonResult getRelationAttrInfoByCid(@PathVariable Long cId){

        List<RelationAttrInfoDTO> list=productAttributeService.getRelationAttrInfoByCid(cId);
        return CommonResult.success(list);
    }

    /**
     * 添加商品属性
     * Request URL: http://localhost:8099/productAttribute/create
     * Request Method: POST
     */
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public CommonResult create(@RequestBody PmsProductAttribute pmsProductAttribute){

        boolean result=productAttributeService.create(pmsProductAttribute);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据ID获取商品属性
     * Request URL: http://localhost:8099/productAttribute/49
     * Request Method: GET
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public CommonResult getById(@PathVariable Long id){

        PmsProductAttribute pmsProductAttribute=productAttributeService.getById(id);
        return CommonResult.success(pmsProductAttribute);
    }

    /**
     * 更新商品属性
     * Request URL: http://localhost:8099/productAttribute/update/49
     * Request Method: POST
     */
    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    public CommonResult updateById(@RequestBody PmsProductAttribute pmsProductAttribute){

        boolean result=productAttributeService.updateById(pmsProductAttribute);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除商品属性
     * Request URL: http://localhost:8099/productAttribute/delete
     * Request Method: POST
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public CommonResult delete(@RequestParam("ids") List<Long> ids){

        boolean result=productAttributeService.delete(ids);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }


}

